#!/bin/sh
#Filename: crmserver-restart.sh
#Description：crm服务重启脚本

if [ ! -n "$1" ] ;then
    echo "请输入版本号!"
    exit
fi

version=$1
sourcebase="/home/jingle/release/crm-server"
basedir="/opt/crm-server/${version}"
dockerfile=""

if [ ! -d $basedir ]; then
  echo "目录 $basedir  不存在,创建目录."
  mkdir -p "$basedir"
else
  echo "删除现有文件..."
  rm -f "${basedir}/"*.war
  rm -f "${basedir}/"*.log
  rm -f "${basedir}/"*.jar
  rm -f "${basedir}/"*.pid
  rm -rf "${basedir}/logs/*"
fi
sleep 3


echo "关闭容器"
docker rmi crm-work-${version}
docker rmi crm-callcenter-${version}
docker rmi crm-customer-${version}
docker rmi crm-config-${version}
docker rmi crm-pms-${version}
docker rmi crm-location-${version}
docker rmi crm-charge-${version}
docker rmi crm-security-${version}

echo "删除容器"
docker rm crm-work-${version}
docker rm crm-callcenter-${version}
docker rm crm-customer-${version}
docker rm crm-config-${version}
docker rm crm-pms-${version}
docker rm crm-location-${version}
docker rm crm-charge-${version}
docker rm crm-security-${version}

echo "删除版本jar镜像"
docker rmi crm-work-${version}
docker rmi crm-callcenter-${version}
docker rmi crm-customer-${version}
docker rmi crm-config-${version}
docker rmi crm-pms-${version}
docker rmi crm-location-${version}
docker rmi crm-charge-${version}
docker rmi crm-security-${version}

echo "构建jar镜像"
docker build -t crm-work-${version} ${basedir}/work/
docker build -t crm-callcenter-${version}
docker build -t crm-customer-${version}
docker build -t crm-config-${version}
docker build -t crm-pms-${version}
docker build -t crm-location-${version}
docker build -t crm-charge-${version}
docker build -t crm-security-${version}

echo "构建redis环境"


echo "构建网络"
docker network create crm-net-${version}

echo "启动容器"


echo "将服务加入网络"
docker network connect --alias work crm-net-${version} crm-work-${version}
docker network connect --alias callcenter crm-net-${version} crm-callcenter-${version}
docker network connect --alias customer crm-net-${version} crm-customer-${version}
docker network connect --alias config crm-net-${version} crm-config-${version}
docker network connect --alias pms crm-net-${version} crm-pms-${version}
docker network connect --alias location crm-net-${version} crm-location-${version}
docker network connect --alias charge crm-net-${version} crm-charge-${version}
docker network connect --alias security crm-net-${version} crm-security-${version}
docker network connect --alias redis crm-net-${version} redis


echo "拷贝jar文件到当前目录..."
cp "${sourcebase}/crm-work/target/crm-work-${version}.jar" "${basedir}/"
cp "${sourcebase}/crm-callcenter/target/crm-callcenter-${version}.jar" "${basedir}/"
cp "${sourcebase}/crm-customer/target/crm-customer-${version}.jar" "${basedir}/"
cp "${sourcebase}/crm-config/target/crm-config-${version}.jar" "${basedir}/"
cp "${sourcebase}/crm-pms/target/crm-pms-${version}.jar" "${basedir}/"
cp "${sourcebase}/crm-location/target/crm-location-${version}.jar" "${basedir}/"
cp "${sourcebase}/crm-charge/target/crm-charge-${version}.jar" "${basedir}/"
cp "${sourcebase}/crm-security/target/crm-security-${version}.jar" "${basedir}/"

echo "启动security.."
service crm-security-${version} restart
sleep 1

echo "启动work.."
service crm-work-${version} restart
sleep 1

echo "启动callcenter.."
service crm-callcenter-${version} restart
sleep 1

echo "启动customer.."
service crm-customer-${version} restart
sleep 1

echo "启动crmconfig.."
service crm-config-${version} restart
sleep 1

echo "启动crmpms.."
service crm-pms-${version} restart
sleep 1

echo "启动crm-location.."
service crm-location-${version} restart
sleep 1

echo "启动crm-charge.."
service crm-charge-${version} restart
sleep 1

echo "服务启动完毕."
